<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="基地" prop="jd">
        <el-input
          v-model="queryParams.jd"
          placeholder="请输入基地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计日期" prop="tjrq">
        <el-input
          v-model="queryParams.tjrq"
          placeholder="请输入统计日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表编号" prop="bbbh">
        <el-input
          v-model="queryParams.bbbh"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Productionline_pricemain:pricemain:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Productionline_pricemain:pricemain:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Productionline_pricemain:pricemain:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Productionline_pricemain:pricemain:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="pricemainList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="创建日期" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="基地" align="center" prop="jd" />
      <el-table-column label="统计日期" align="center" prop="tjrq" />
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="250px"
      >
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
          >导出</el-button            >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_pricemain:pricemain:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_pricemain:pricemain:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改制程检验原辅料消耗在线报废留样统计对话框 -->
    <!-- <el-dialog v-myFullscreen
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
    > -->
    <el-dialog v-myFullscreen
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
      custom-class="myDialog"
      fullscreen
    >
      <TableHeader
        @nameChange="nameChange"
        :data="{
          mainTitle: '制程检验原辅料消耗在线报废留样统计',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="基地" prop="jd">
              <el-input
                v-model="form.jd"
                placeholder="请输入基地"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="统计日期" prop="tjrq">
              <el-input
                v-model="form.tjrq"
                placeholder="请输入统计日期"
                :disabled="openDisabled"
              />
              <!-- <el-date-picker  :editable="false"
                clearable
                v-model="form.tjrq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择统计日期"
                :disabled="openDisabled"
              >
              </el-date-picker> -->
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="报表编号" prop="bbbh">
              <el-input
                v-model="form.bbbh"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <br>
        <el-divider content-position="center"
          >制程检验原辅料消耗在线报废留样统计信息</el-divider
        >
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddQualityProdSamplePriceSub"
              :disabled="openDisabled"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteQualityProdSamplePriceSub"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="qualityProdSamplePriceSubList"
          :row-class-name="rowQualityProdSamplePriceSubIndex"
          @selection-change="handleQualityProdSamplePriceSubSelectionChange"
          ref="qualityProdSamplePriceSub"
          max-height="550"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column width="200px" label="品项" prop="pxId">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.pxId"
                placeholder="请输入品项"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="瓶坯数量" prop="psum">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.psum"
                placeholder="请输入瓶坯数量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="瓶盖数量" prop="gsum">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.gsum"
                placeholder="请输入瓶盖数量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="商标数量" prop="sbsum">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sbsum"
                placeholder="请输入商标数量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="在线" prop="zx">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.zx"
                placeholder="请输入在线"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="报废" prop="bf">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.bf"
                placeholder="请输入报废"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="留样" prop="ly">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.ly"
                placeholder="请输入留样"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="单价" prop="dj">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.dj"
                placeholder="请输入单价"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="在线费用" prop="zxfy">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.zxfy"
                placeholder="请输入在线费用"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="报废费用" prop="bffy">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.bffy"
                placeholder="请输入报废费用"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="留样费用" prop="lyfy">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.lyfy"
                placeholder="请输入留样费用"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="价格" prop="price">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.price"
                placeholder="请输入价格"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="报表编号" prop="bbbh">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.bbbh"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column width="200px" label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                type="textarea"
                placeholder="请输入备注"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPricemain,
  getPricemain,
  delPricemain,
  addPricemain,
  updatePricemain,
} from "@/api/quality/productionline/pricemain";

export default {
  name: "Pricemain",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQualityProdSamplePriceSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 制程检验原辅料消耗在线报废留样统计表格数据
      pricemainList: [],
      // 制程检验原辅料消耗在线报废留样统计表格数据
      qualityProdSamplePriceSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        jd: null,
        tjrq: null,
        bbbh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询制程检验原辅料消耗在线报废留样统计列表 */
    getList() {
      this.loading = true;
      listPricemain(this.queryParams).then((response) => {
        this.pricemainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jd: null,
        tjrq: null,
        bbbh: 'LK-QF-LS1-10-015-11-05',
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.qualityProdSamplePriceSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加制程检验原辅料消耗在线报废留样统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getPricemain(id).then((response) => {
        this.form = response.data;
        this.qualityProdSamplePriceSubList =
          response.data.qualityProdSamplePriceSubList;
        this.open = true;
        this.title = "修改制程检验原辅料消耗在线报废留样统计";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getPricemain(id).then((response) => {
        this.form = response.data;
        this.qualityProdSamplePriceSubList =
          response.data.qualityProdSamplePriceSubList;
        this.open = true;
        this.title = "查看制程检验原辅料消耗在线报废留样统计";
      });
    },


    excelhandleView(row){
      this.download(
        "Productionline/pricemain/exportexcel?id="+row.id,
        {
          ...this.queryParams,
        },
        "制程检验原辅料消耗在线报废留样统计" + this.parseTime(new Date()) + ".xlsx"
      );
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {

        if (valid) {
          this.form.qualityProdSamplePriceSubList =
            this.qualityProdSamplePriceSubList;
            /**打开遮罩层*/
            const rLoading = this.openLoading();
          if (this.form.id != null) {
            updatePricemain(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          } else {
            addPricemain(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除制程检验原辅料消耗在线报废留样统计编号为"' +
          ids +
          '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delPricemain(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 制程检验原辅料消耗在线报废留样统计序号 */
    rowQualityProdSamplePriceSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 制程检验原辅料消耗在线报废留样统计添加按钮操作 */
    handleAddQualityProdSamplePriceSub() {
      let obj = {};
      obj.pxId = "";
      obj.psum = "";
      obj.gsum = "";
      obj.sbsum = "";
      obj.zx = "";
      obj.bf = "";
      obj.ly = "";
      obj.dj = "";
      obj.zxfy = "";
      obj.bffy = "";
      obj.lyfy = "";
      obj.price = "";
      obj.bbbh = "";
      obj.remark = "";
      this.qualityProdSamplePriceSubList.push(obj);
    },
    /** 制程检验原辅料消耗在线报废留样统计删除按钮操作 */
    handleDeleteQualityProdSamplePriceSub() {
      if (this.checkedQualityProdSamplePriceSub.length == 0) {
        this.$alert(
          "请先选择要删除的制程检验原辅料消耗在线报废留样统计数据",
          "提示",
          { confirmButtonText: "确定" }
        );
      } else {
        this.qualityProdSamplePriceSubList.splice(
          this.checkedQualityProdSamplePriceSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleQualityProdSamplePriceSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.qualityProdSamplePriceSub.clearSelection();
        this.$refs.qualityProdSamplePriceSub.toggleRowSelection(
          selection.pop()
        );
      } else {
        this.checkedQualityProdSamplePriceSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Productionline_pricemain/pricemain/export",
        {
          ...this.queryParams,
        },
        "制程检验原辅料消耗在线报废留样统计" +
          this.parseTime(new Date()) +
          ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
  },
};
</script>
